package com.donger.business.upms.global;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.donger.auth.component.UserDetail;
import com.donger.auth.utils.SecurityUtils;
import com.donger.business.upms.entity.SysConfig;
import com.donger.business.upms.entity.SysDictItem;
import com.donger.business.upms.mapper.SysConfigMapper;
import com.donger.business.upms.mapper.SysDictItemMapper;
import com.donger.common.data.entity.DropDown;
import com.donger.common.data.service.SysBaseApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 7326
 */
@Service(value = "sysBaseApi")
public class SysBaseApiImpl implements SysBaseApi {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Override
    public String getConfig(String key) {
        SysConfig sysConfig = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, key)
        );
        return sysConfig != null ? sysConfig.getParamValue() : null;
    }

//    @Override
//    public UserDetail getUser() {
//        return SecurityUtils.getUser();
//    }

    @Override
    public List<DropDown> getDictListByKey(String key) {
        return sysDictItemMapper.selectList(Wrappers.<SysDictItem>lambdaQuery()
                .eq(SysDictItem::getType, key)
        ).stream().map(item -> new DropDown().setLabel(item.getItemText()).setValue(item.getItemCode()))
                .collect(Collectors.toList());
    }

    @Override
    public String getDictBykey(String item, String key) {
        SysDictItem sysDictItem = sysDictItemMapper.selectOne(Wrappers.<SysDictItem>lambdaQuery()
                .eq(SysDictItem::getType, item)
                .eq(SysDictItem::getItemCode, key)
        );
        return sysDictItem.getItemText();
    }
}
